//#ifdef MP-WEIXIN
const baseUrl = "http://192.168.0.66:8086/api"
//#endif

//#ifdef H5
// const baseUrl = "/api"
const baseUrl = "/schoolPeExamApi"
// const baseUrl = "/api"
console.log('运行在浏览器H5环境下');
//#endif

// 参数 "options" 从接口函数传递过来
const request = (options) => {
	return new Promise((resolve, reject) => {
		// 拼接请求地址
		options.url = baseUrl + options.url
		uni.showLoading({
			title: '加载中'
		});

		let loginInfo = null;
		let token = ""
		let loginInfoStr = uni.getStorageSync('loginInfo');
		if (loginInfoStr != null && loginInfoStr != '') {
			loginInfo = JSON.parse(loginInfoStr);
		}
		if (loginInfo != null) {
			console.log("loginInfo.token");
			console.log(loginInfo.token);
			token = loginInfo.token;
		}


		if (options.header == undefined) {
			options.header = {
				'Access-Control-Allow-Origin': '*',
				'Access-Control-Allow-Headers': 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
				'Authorization': token,
				'content-type': 'application/json',
				'cache-control': 'no-cache',
				'Access-Control-Allow-Headers': 'appId',
				'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
				'Access-Control-Max-Age': 86400,
			}
		}
		uni.request({
			url: options.url,
			method: options.method,
			data: options.data,
			header: options.header,

			success: (res) => {
				//隐藏加载框
				uni.hideLoading();
				console.log(".................");
				console.log(res.data.code);
				if(res.data.code == 1000){
					uni.setStorage({
						key: 'loginInfo',
						data: null
					})
					uni.setStorage({
						key: 'userInfo',
						data: null
					})
					console.log("loginOut");
					uni.navigateTo({
						url:"/pages/accountManage/accountManage"
					});
					uni.showToast({
						icon:"error",
						title:"请重新登录"
					})
				}
				resolve(res.data)
			},
			fail: (res) => {
				//隐藏加载框
				uni.hideLoading();
				console.log(res);
				reject(res)
			},
		});
	})
}
export default request